package as;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Servlet implementation class LoginServ
 */
@WebServlet("/LoginServ")
public class LoginServ extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//private static final String ID = null;
	//private static final String Password = null;

    /**
     * @see HttpServlet#HttpServlet()
     */
	public LoginServ() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub]
		String ID = request.getParameter("rid");
		String Password = request.getParameter("pwd");
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs=null;


		String url = "jdbc:mysql://localhost:3306/shangpin";
		String user = "root";
		String password = "rosy";

		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			conn = DriverManager.getConnection(url, user, password);


		 String sql = "select * from userinfo where ID =? and Password=?";
		 pstmt = conn.prepareStatement(sql);
		 pstmt.setString(1, ID);
		 pstmt.setString(2, Password);
	 rs=pstmt.executeQuery();
	 	if(rs.next()){
	 		response.sendRedirect("jsp/main1.jsp");
	 	}else{response.sendRedirect("jsp/loginfail.jsp");
	 	pstmt.close();
 		conn.close();
 		rs.close();
	 	}
		} catch (SQLException e) {
			// TODO 自動生成された catch ブロック
			System.out.println("Sql exception");
		} catch (ClassNotFoundException e) {
			System.out.println("Class not found");

		}
		catch (Exception e) {
			System.out.println("exception");
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				response.getWriter().println(e.getMessage());
			}
		}

	 }
}
